/*********************************************************
 * copyright xinbohit.com 版权所有 
 * 开发人员：IvanYu
 * 创建时间：2014/10/28 2:02:27
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.WebSite.Admin
{
    /// <summary>
    /// CustomSectionList1 的摘要说明
    /// </summary>
    public class CustomSectionList1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string result;
            string sSearch = context.Request.Params["sSearch"];
            string iDisplayStartStr = context.Request.Params["iDisplayStart"];
            string iDisplayLengthStr = context.Request.Params["iDisplayLength"];
            int iDisplayStart = 0;
            int iDisplayLength = 0;
            int.TryParse(iDisplayStartStr, out iDisplayStart);
            int.TryParse(iDisplayLengthStr, out iDisplayLength);

            try
            {
                using (CustomSectionLogic logic = new CustomSectionLogic())
                {
                    int total = 0;
                    List<CustomSection> msgs = logic.GetListByPage2(out total, p => p.Title.Contains(sSearch), p => p.CreatedTime, BLL.Enums.OrderEnum.Descending, iDisplayStart, iDisplayLength);

                    var messages = from d in msgs
                                   select new
                                   {
                                       Id = d.Id,
                                       Title = d.Title,
                                       Content = d.Content,
                                       CreatedTime = d.CreatedTime.ToString(),
                                   };
                    result = serializer.Serialize(new
                    {
                        sEcho = context.Request["sEcho"],
                        iTotalRecords = total,
                        iTotalDisplayRecords = total,
                        aaData = messages
                    });
                }
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                result = serializer.Serialize(new
                {
                    sEcho = context.Request["sEcho"],
                    iTotalRecords = 0,
                    iTotalDisplayRecords = 0,
                    aaData = new List<CustomSection>()
                });
            }
            context.Response.Write(result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}